using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class DanhMucMon : System.Web.UI.Page
{
    FastFoodBO foodBO = new FastFoodBO();

    // Code for Paging

    #region Paging

    PagedDataSource pds = new PagedDataSource();
    private int pageSize = 6;
    private string maloai;

    public int CurrentPage
    {
        get
        {
            if (ViewState["CurrentPage"] == null)
                return 0;
            else
                return Convert.ToInt16(ViewState["CurrentPage"].ToString());
        }
        set
        {
            ViewState["CurrentPage"] = value;
        }
    }

    private int NumberOfPage
    {
        get
        {
            return (int)ViewState["NumberOfPage"];
        }
        set
        {
            ViewState["NumberOfPage"] = value;
        }
    }


    private List<FastFoodEntity> ListMonAn
    {
        get
        {
            if (ViewState["MonAn"] != null)
            {
                return (List<FastFoodEntity>)ViewState["MonAn"];
            }
            else
            {
                return new List<FastFoodEntity>();
            }
        }
        set { ViewState["MonAn"] = value; }
    }

    private void BindGrid(List<FastFoodEntity> list)
    {
        pds.DataSource = list;
        pds.AllowPaging = true;
        pds.PageSize = pageSize;
        pds.CurrentPageIndex = CurrentPage;
        lnkbtnNext.Enabled = !pds.IsLastPage;
        lnkbtnPrevious.Enabled = !pds.IsFirstPage;

        dataListSP.DataSource = pds;
        dataListSP.DataBind();

        doPaging();
    }


    private void doPaging()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("PageIndex");
        dt.Columns.Add("PageText");
        for (int i = 0; i < pds.PageCount; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = i;
            dr[1] = i + 1;
            dt.Rows.Add(dr);
        }

        dlPaging.DataSource = dt;
        dlPaging.DataBind();
    }

    protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName.Equals("lnkbtnPaging"))
        {
            CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
            BindGrid(ListMonAn);
        }
    }

    protected void lnkbtnPrevious_Click(object sender, EventArgs e)
    {
        CurrentPage -= 1;
        BindGrid(ListMonAn);
    }

    protected void lnkbtnNext_Click(object sender, EventArgs e)
    {
        CurrentPage += 1;
        BindGrid(ListMonAn);
    }

    protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
        if (lnkbtnPage.CommandArgument == CurrentPage.ToString())
        {
            lnkbtnPage.Enabled = false;
            lnkbtnPage.Font.Bold = true;
        }
    }

    #endregion
    // end code for paging
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<FastFoodEntity> list = ConvertTableToList(GetData());
            ListMonAn = new List<FastFoodEntity>(list);
            BindGrid(list);
        }
        else
        {
            BindGrid(ListMonAn);
        }
    }

    private DataTable GetData()
    {
        DataTable sanPhamBO = new DataTable("FastFood");
        sanPhamBO = foodBO.GetAllMon();
        return sanPhamBO;
    }

    private List<FastFoodEntity> ConvertTableToList(DataTable table)
    {
        List<FastFoodEntity> list = new List<FastFoodEntity>();
        foreach (DataRow row in table.Rows)
        {
            FastFoodEntity entity = new FastFoodEntity();
            long foodId;
            long.TryParse(row["FoodId"].ToString(), out foodId);
            entity.FoodId = foodId;
            entity.Name = row["Name"].ToString();
            decimal cost;
            decimal.TryParse(row["Cost"].ToString(), out cost);
            entity.Cost = cost;
            entity.Unit = row["Unit"].ToString();
            entity.Image = row["Image"].ToString();
            long inventory;
            long.TryParse(row["Inventory"].ToString(), out inventory);
            entity.Inventory = inventory;
            long labelId;
            long.TryParse(row["LabelId"].ToString(), out labelId);
            entity.LabelId = labelId;
            //short isDelete;
            //short.TryParse(row["IsDelete"].ToString(),out isDelete);
            //entity.IsDelete = isDelete;
            long foodType;
            long.TryParse(row["FoodTypeId"].ToString(), out foodType);
            entity.FoodTypeId = foodType;
            list.Add(entity);
        }
        return list;
    }

    protected void dataListSP_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
